#include <iostream>
#include <vector>
using namespace std;


class DisjointSet{
public:
	DisjointSet(int num):arr(num){
		for (int i = 0; i < arr.size(); ++i)
		{
			arr[i] = -1;
		}
	
	}
	void unionSets(int r1, int r2)
	{
		arr[r2] = r1;
	}
	
	void unionSets2(int r1, int r2)
	{
		if (arr[r1] < arr[r2]) //r1 is deeper
		{
			arr[r2] = r1;
		}
		else if (arr[r2] < arr[r1])
		{
			arr[r1] = r2;
		}
		else{
			--arr[r1];
			arr[r2] = r1;
		}


	}
	
	int find(int x)
	{
		if (arr[x] < 0)
			return x;
		return find(arr[x]);

	}

	int find2(int x)
	{
		if (arr[x] < 0)
			return x;
		return arr[x] = find(x);
	}


	void desc()
	{
		for (int i = 0; i < arr.size(); ++i)
		{
			cout << i << " " << arr[i] << endl;
		}
	}

private:
	vector<int> arr;



};

int main()
{
	DisjointSet ds(20);
	
	int x, y;


	while (cin >> x)
	{
		if (x == 12306)
		{
			cout << "find: ";
			cin >> x;
			cout << " in set: "<<ds.find2(x) << endl;
		}
		else if (x == 12312)
		{
			cout << "union ";
			cin >> x;
			cout << " and ";
			cin >> y;
			ds.unionSets2(x, y);
		}


		else if(x == 10086){
			ds.desc();
		}
		
	}


	return 0;
}










